package com.jy.appmarket.screenlock.activity;

import com.jy.appmarket.R;
import com.jy.appmarket.dialog.TextDialog;
import com.jy.appmarket.screenlock.LockScreenManager;
import com.jy.appmarket.screenlock.prefs.LockPrefs;
import com.jy.appmarket.screenlock.statistics.SwitchEvent;
import com.jy.appmarket.screenlock.utils.LockScreenConfig;
import com.jy.appmarket.utils.NetTools;
import com.rad.statistics.android.StatisticsAgent;
import com.umeng.analytics.MobclickAgent;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * 类说明：
 * 
 * @author jie.li
 * @date 2015-3-4
 * @version 1.0
 */
public class BeautifulLockerSettingActivity extends Activity implements OnClickListener {

	private LinearLayout mInitSettingLayout;

	private boolean isStartLocker = false;
	private boolean isOpenLockSound = false;
	private boolean isWIfiDownloadWallPaper;

	private CheckBox mStartLocker;
	private CheckBox mLockSound;
	private CheckBox mWifiDownloadWallPaper;

	private Button mExperienceLockScreenBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_beautiful_locker_setting);

		initView();

		// 线上包触发
		if ("XS".equals(NetTools.getChannelType(this)) && !LockPrefs.isInitLockSetting(this)
				&& LockPrefs.isFirstEnterMyLock(this)) {
			LockPrefs.setFirstEnterMyLock(this, false);
			Toast.makeText(this, "锁屏已启用", Toast.LENGTH_SHORT).show();
			showInitLockSettingDialog();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		isStartLocker = LockPrefs.getStartLocker(this);
		isOpenLockSound = LockPrefs.isLockSoundOpen(this);
		isWIfiDownloadWallPaper = LockScreenConfig.isWallpaperOfWifi(this);

		mStartLocker.setChecked(isStartLocker);
		mLockSound.setChecked(isOpenLockSound);
		mWifiDownloadWallPaper.setChecked(isWIfiDownloadWallPaper);
	}

	private void initView() {
		initTitle();
		findViewById(R.id.setting_unlock_password_layout).setOnClickListener(this);

		mExperienceLockScreenBtn = findViewById(R.id.experience_lock_screen_btn);
		mExperienceLockScreenBtn.setOnClickListener(this);

		mInitSettingLayout = findViewById(R.id.init_setting_layout);

		mInitSettingLayout.setOnClickListener(this);
		mStartLocker = findViewById(R.id.start_locker_switch);
		mStartLocker.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LockPrefs.setStartLocker(BeautifulLockerSettingActivity.this, isChecked);
				LockScreenManager mgr = LockScreenManager.getInstance(buttonView.getContext());
				if (isChecked) {
					mgr.disableSystemLock();
				} else {
					mgr.enableSystemLock();
				}
				// 锁屏开关统计
				sendStatistics("start_lock_screen_event", isChecked);
			}
		});

		mWifiDownloadWallPaper = findViewById(R.id.wifi_download_wallpaper_switch);
		mWifiDownloadWallPaper.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LockScreenConfig.setWallpaperOfWifi(BeautifulLockerSettingActivity.this, isChecked);
				sendStatistics("open_wifi_download_wallpaper_event", isChecked);
			}
		});

		mLockSound = findViewById(R.id.lock_sound_switch);
		mLockSound.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				LockPrefs.setLockSoundOpen(BeautifulLockerSettingActivity.this, isChecked);
				sendStatistics("open_lock_sound_event", isChecked);
			}
		});

	}

	/**
	 * eventName ： 启动锁屏-->start_lock_screen_event (true:开启，false:关闭)
	 * 显示寄语-->show_wishes_event (true:显示，false:不显示) 壁纸类型-->wallpaper_type_event
	 * (true:自定义，false:自动更新) 启动声音-->open_lock_sound_event (true:开启，false:关闭)
	 * 
	 * @param eventName
	 * @param open
	 */
	private void sendStatistics(String eventName, boolean open) {
		SwitchEvent event = new SwitchEvent();
		event.setEventName(eventName);
		event.setStarted(open);
		StatisticsAgent.onEvent(this, event);
	}

	private void initTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("锁屏设置");
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
	}

	private void finishOnResult() {
		if ((isStartLocker != LockPrefs.getStartLocker(this)) || isOpenLockSound != LockPrefs.isLockSoundOpen(this)
				|| isWIfiDownloadWallPaper != LockScreenConfig.isWallpaperOfWifi(this)) {
			Toast.makeText(this, "已设置成功", Toast.LENGTH_LONG).show();
		}
		finish();
	}

	@Override
	public void onBackPressed() {
		finishOnResult();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.init_setting_layout:
			startActivity(new Intent(this, LockInitSettingActivity.class));
			break;

		case R.id.setting_unlock_password_layout:// 设置解锁密码
			startActivity(new Intent(this, SetLockerPasswordActivity.class));
			break;

		case R.id.experience_lock_screen_btn:
			LockScreenManager.getInstance(this).lock();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finishOnResult();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void showInitLockSettingDialog() {
		final TextDialog dialog = new TextDialog(this);
		dialog.showTitle(R.string.prompt).setContent(R.string.init_lock_setting_content)
				.showButton1(getString(R.string.cancel), new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.cancel();
					}
				}).showButton2(getString(R.string.init_lock_setting_confirm), new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.cancel();
						startActivity(new Intent(BeautifulLockerSettingActivity.this, LockInitSettingActivity.class));
					}
				}).show();
	}
}
